const { SuccessModel, ErrorModel } = require("../model/responseModel");
const { getBlogList, getBlogDetail, createNewBlog, updateBlog, deleteBlog } = require("../controllers/blog");
const { execSQL } = require("../db/mysql");


// 处理博客相关的路由
const handleBlogRoute = (req, res) => {
    //设置相应格式
    const method = req.method;
    const id = req.query.id;
    const blogData = req.body;


    if (method === "GET" && req.path === '/api/blog/list') {

        const author = req.query.author || ''
        const keyword = req.query.keyword || ''
        const listDataPromise = getBlogList(author, keyword)
        return listDataPromise.then((listData) => {
            return new SuccessModel(listData)

        })

    }
    //获取博客详情接口
    if (method === "GET" && req.path === '/api/blog/dateil') {
        const detailDataPromise = getBlogDetail(id);
        return detailDataPromise.then(datailData => {
            return new SuccessModel(datailData)
        })
    }

    //新建一篇博客
    if (method === "POST" && req.path === '/api/blog/new') {
        const author = 'zhangsan';
        req.body.author = author;
        const newBlogDataPromise = createNewBlog(blogData)

        return newBlogDataPromise.then(newBlogData => {
            return new SuccessModel(newBlogData);
        })
    }


    //更新博客
    if (method === "POST" && req.path === '/api/blog/update') {
        const updatedBlogDataPromise = updateBlog(id, blogData)
        return updatedBlogDataPromise.then(updatedBlogData => {

        })




    }

    //删除博客
    if (method === "POST" && req.path === '/api/blog/del') {
        const author = 'zhangsan'
        const deleteBlogPromise = deleteBlog(id, author);
        return deleteBlogPromise.then((deleteBlogData) => {
            if (deleteBlogData) {
                return new SuccessModel("删除博客成功")
            } else {
                return new ErrorModel("删除博客失败")
            }
        })



    }
}
module.exports = handleBlogRoute;